<template>
  <el-dialog
    :title="form.id?(isDetail?'详情':'编辑'):'新增'"
    :visible.sync="dialogVisible"
    :before-close="close"
    width="40%"
  >
    <el-form :disabled="isDetail" label-width="100px">
      <el-form-item label="轮播图">
        <el-upload
          action="http://10.4.10.50:9010/file/upload"
          :auto-upload="true"
          :multiple="false"
          :limit="1"
          class="avatar-uploader"
          :show-file-list="false"
          :before-upload="handleBeforeUpload"
          :on-success="handleUploadSuccess"
          :on-error="handleUploadError"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="标题">
        <el-input v-model="form.title" size="mini" />
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number
          v-model="form.sort"
          controls-position="right"
          size="mini"
          :min="0"
        />
      </el-form-item>
      <el-form-item label="显示状态">
        <el-switch size="mini" v-model="form.isShow" />
      </el-form-item>
      <el-form-item label="跳转">
        <el-radio-group v-model="form.isJump">
          <el-radio label="1">外部链接</el-radio>
          <el-radio label="2">内部文章</el-radio>
          <el-radio label="3">不跳转</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="form.isJump==='1'" label="外部链接">
        <el-input v-model="form.jumpLink" size="mini" />
      </el-form-item>
      <el-form-item v-if="form.isJump==='2'" label="文章ID">
        <el-input v-model="form.articleId" size="mini" />
      </el-form-item>
      <el-form-item label="详情">
        <el-input
          v-model="form.content"
          size="mini"
          :rows="3"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <span v-if="!isDetail" slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getToken } from '@/utils/auth'
import { add, edit } from '@/api/cadreBusiness/carouselChart'
export default {
  data () {
    return {
      dialogVisible: false,
      form: {},
      isDetail: false,
      headers: {
        Authorization: 'Bearer ' + getToken()
      }
    }
  },
  methods: {
    open (row, isDetail) {
      this.isDetail = isDetail
      this.form = { ...row }
      this.dialogVisible = true
    },
    confirm () {
      let submitForm = add
      if (this.form.id) {
        submitForm = edit
      }
      if (!this.form.scopeSending) {
        this.form.scopeSending = 0
      }
      submitForm(this.form).then((res) => {
        this.$message.success('保存成功')
        this.$emit('getData')
        this.close()
      })
    },
    close () {
      this.form = {}
      this.dialogVisible = false
    },
    // 上传前校检格式和大小
    handleBeforeUpload (file) {
      // 校检文件大小
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize
        if (!isLt) {
          this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`)
          return false
        }
      }
      return true
    },
    handleUploadSuccess (res, file) {
      console.log('res', res)
      console.log('file', file)
    },
    handleUploadError () {
      this.$message.error('图片上传失败')
    }
  }
}
</script>
<style lang="scss" scoped>
  .titleStyle {
    font-weight: 600;
    height: 35px;
    line-height: 35px;
    padding-left: 10px;
    padding-right: 10px;
    margin-bottom: 8px;
    background-color: #d8eff9;
    display: flex;
    justify-content: space-between;
  }
  .upload-demo {
    display: inline-block !important;
    margin-left: 16px;
  }
  .avatar-uploader {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    width: 240px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader :hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 240px;
    height: 180px;
    line-height: 180px;
    text-align: center;
  }
  .avatar {
    width: 240px;
    height: 180px;
    display: block;
  }
  .upload-tip {
    height: 22px;
    line-height: 22px;
  }
</style>
